/* eslint-disable promise/param-names */
<template>
  <div class="info-container">
    <el-card style="height: 100%">
      <search-tool @onSearch="handleSearch" :rowData="sele"></search-tool>
      <div class="btns">
        <div class="add-btn" @click="handleAdd">
          <!-- <svg-icon icon="add" /> -->
          <span class="addFont">新增</span>
        </div>
      </div>
      <el-table v-loading="loading" :data="tableData" style="width: 100%">
        <el-table-column label="序号" type="index" width="80" fixed="left" />
        <el-table-column
          prop="nickName"
          label="用户名称"
          fixed="left"
          width="150"
        >
        </el-table-column>
        <el-table-column prop="roleName" label="所属角色" width="120">
        </el-table-column>
        <el-table-column prop="deptName" label="所属部门" width="180">
        </el-table-column>
        <el-table-column prop="gender" label="性别"> </el-table-column>
        <el-table-column prop="mobile" label="联系电话" width="160">
        </el-table-column>
        <el-table-column prop="name" label="账户名称"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="150">
        </el-table-column>
        <el-table-column
          prop="updateTime"
          label="更新时间"
          width="200"
        ></el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button
              type="text"
              size="small"
              @click="handleEdit(row)"
              style="color: #0052d9"
              >修改</el-button
            >
            <div class="has-more"></div>
            <el-button
              type="text"
              size="small"
              @click="handleExamine(row)"
              style="color: #0052d9"
              >重置</el-button
            >
            <div class="has-more"></div>
            <el-button
              type="text"
              size="small"
              @click="handleView(row)"
              style="color: #e34d59"
              >删除</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="启停状态">
          <template #default="{ row }">
            <el-switch
              v-model="row.state"
              :active-value="true"
              :inactive-value="false"
              @change="beforeChange2(row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        layout="prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <AddDialog
      :title="title"
      :rowData="curRowData"
      v-model:visible="visibleViewDialog"
      :user="sele"
      @onReload="reloadPage"
    ></AddDialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { userList, roleList, deleuser } from '@/api/user'
import SearchTool from './components/SearchTool/index.vue'
import AddDialog from './components/AddDialog/index.vue'

import { ElMessage, ElMessageBox } from 'element-plus'
//数据相关
const tableData = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(10)
const params = ref({})
const loading = ref(false)
const title = ref('新增角色')
const value1 = ref()
const sele = ref([])
const curRowData = ref({})
const visibleViewDialog = ref(false)
// 所属角色数据
const role = async () => {
  const res = await roleList()
  sele.value = res
}
role()
const getListData = async (params) => {
  loading.value = true
  const result = await userList(params, page.value, size.value)
  loading.value = false
  tableData.value = result.rows
  total.value = result.total
}

const reloadPage = () => {
  getListData(params.value)
  if (visibleViewDialog.value) {
    visibleViewDialog.value = false
  }
}

reloadPage()
//查询条件相关
const handleSearch = (paramsData) => {
  params.value = paramsData
  getListData(paramsData)
}

// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData(params.value)
}

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData(params.value)
}

// 账户状态
const beforeChange2 = (row) => {
  let flag = row.state
  row.state = !row.state //保持switch点击前的状态
  title.value = '用户'
  ElMessageBox.confirm('是否确认此操作', '操作', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      if (flag) {
        row.state = true
        ElMessage({
          type: 'success',
          message: '打开成功!'
        })
      } else {
        row.state = false
        ElMessage({
          type: 'success',
          message: '关闭成功!'
        })
      }
      curRowData.value = row
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消操作！'
      })
    })
}
// 新增用户
const handleAdd = () => {
  title.value = '新增用户'
  visibleViewDialog.value = true
}
// 修改用户
const handleEdit = (row) => {
  title.value = '修改用户'
  visibleViewDialog.value = true
  curRowData.value = row
}
// 刪除用戶
const handleView = (row) => {
  ElMessageBox.confirm('确定删除此用户吗？', '操作', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await deleuser(row.id).then(() => {
        ElMessage({
          type: 'success',
          message: '删除成功!'
        })
        reloadPage()
      })
    })
    .catch(() => {
      return false
    })
}
</script>

<style lang="scss" scoped>
.info-container {
  width: 100%;
  .btns {
    position: relative;
    width: 100%;
    display: flex;
    height: 40px;
    align-items: center;
    margin-bottom: 20px;
    .add-btn {
      width: 74px;
      height: 36px;
      font-family: Microsoft YaHei;
      color: #ffffff;
      background-color: #547bf1;
      background: #1e6ae7;
      border: 1px solid #8bb7ff;
      cursor: pointer;
      position: relative;
      .addFont {
        position: absolute;
        font-size: 14px;
        left: 21px;
        top: 7px;
        // font-size: 14px;
      }
      // border-radius: 5px;
      // text-align: center;
      // line-height: 36px;
      // padding-left: 15px;
      // .svg-icon{
      //   position: absolute;
      //   top: 10px;
      //   left: 10px;
      // }
    }
  }
  .has-more {
    display: inline-block;
    width: 2px;
    height: 10px;
    background: #0052d9;
    margin-right: 10px;
    margin-left: 10px;
    margin-top: 5px;
  }
  .pagination {
    width: 100%;
    height: 32px;
    margin-top: 10px;
    display: flex;
    align-items: center;
    justify-content: end;
  }
}
:deep(.el-dialog__body) {
  padding: 30px 30px 0px;
}
</style>
